package controllers;

import models.Photo;
import play.Play;
import play.cache.Cache;
import play.mvc.With;
import utils.Pager;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Cyril
 * Date: 08.08.11
 * Time: 0:40
 * To change this template use File | Settings | File Templates.
 */
@With(UserChecker.class)
public class Photos extends MenuBuilder {
    public static void index() {
        Integer s = request.params.get("s", Integer.class);
        if (s == null) {
            if (session.contains("photos-pager-size"))
                s = Integer.valueOf(session.get("photos-pager-size"));
            else
                s = 16;
        } else
            session.put("photos-pager-size", s);
        Integer p = request.params.get("p", Integer.class);
        p = p == null ? 1 : p;
        List<Photo> photos = Photo.all()
            .fetch(s, (p - 1) * s);
        renderArgs.put("pager", new Pager(p, Photo.all().count() / s));

        Integer lvl = request.params.get("l", Integer.class);
        if (lvl != null && lvl == -1) {
            lvl = null;
            Cache.delete("photo-size-level");
        }
        if (lvl == null) {
            lvl = Cache.get("photo-size-level", Integer.class);
            if (lvl == null)
                lvl = Integer.valueOf(
                        Play.configuration.getProperty("photo.size.level"));
        } else
            Cache.set("photo-size-level", lvl);
        renderArgs.put("lvl", lvl);
        renderArgs.put("photosInLine",
                Integer.valueOf(Play.configuration.getProperty("photo.page.size")));
        render(photos);
    }
}
